//
//  AliMNSResponse.m
//  AlibabaCloudMNSiOS
//
//  Created by David Chen on 28/03/2017.
//  Copyright © 2017 AliCloud. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AliMNSResponse.h"
#import "AliMNSHttpBodyDecoder.h"
#import "AliMNSException.h"

@implementation AliMNSResponseBase

@synthesize internalError;
@synthesize header;

@synthesize code;
@synthesize errorMessage;
@synthesize requestId;
@synthesize hostId;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    requestId = [res.header objectForKey:@"x-mns-request-id"];
    internalError = res.error;
}

-(void) parseErrorCode:(AliMNSHttpResponseInternal*) res
{
    NSString* icode = [NSString new];
    NSString* imessage = [NSString new];
    NSString* irId = [NSString new];
    NSString* ihostId = [NSString new];

    [AliMNSHttpBodyDecoder getErrorCodeFromXml:res.data withOutputCode: &icode withOutputMessage:&imessage withOutputRequestId:&irId withOutputHostId:&ihostId];

    code = icode;
    errorMessage = imessage;
    requestId = irId;
    hostId = ihostId;
}

-(void) checkError:(AliMNSHttpResponseInternal*) res
{
    if(internalError != nil)
    {
        AliMNSClientNetworkException* cne = [[AliMNSClientNetworkException alloc] initWithError:internalError];
        @throw cne;
    }

    if(code != nil)
    {
        AliMNSServerException* se = [[AliMNSServerException alloc] initWithCode:code withMessage:errorMessage withRequestId:requestId withHostId:hostId];
        se.httpStatus = res.status;
        @throw se;
    }
}

-(AliMNSError*) generateError
{
    AliMNSError* e = nil;
    if(internalError != nil)
    {
        e = [AliMNSError new];
        e.errorCode = @"NetworkError";
        e.errorMessage = [internalError localizedDescription];
    }else if(code != nil)
    {
        e = [AliMNSError new];
        e.errorCode = code;
        e.errorMessage = errorMessage;
        e.requestId = requestId;
        e.hostId = hostId;
    }
    return e;
}

@end

@implementation AliMNSCreateQueueResponse
@synthesize queueUrl;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];

    if ([res.status isEqual:@"201"] || [res.status isEqual:@"204"])
    {
        queueUrl = [res.header objectForKey:@"Location"];
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSDeleteQueueResponse

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];

    if ([res.status isEqual: @"204"])
    {
        //NSLog(@"%@ Delete Queue successfully!\n", res.status);
    }
    else
    {
        [super parseErrorCode:res];
    }
}

@end

@implementation AliMNSListQueueResponse

@synthesize nextMarker;
@synthesize queueMetaList;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        //parse the interal request.
        NSString* marker = @"";
        NSMutableArray * qMetaList = [NSMutableArray new];
        [AliMNSHttpBodyDecoder getQueueListFromXml:res.data withOutputMarker:&marker withOutputQueueMetaList:&qMetaList];
        nextMarker = marker;
        queueMetaList = [qMetaList copy];

    }else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSSetQueueAttributesResponse
-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"204"])
    {
    }
    else
    {
        [super parseErrorCode:res];
    }

}

@end

@implementation AliMNSGetQueueAttributesResponse

@synthesize queueMeta;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];

    if ([res.status isEqual: @"200"])
    {
        queueMeta = [AliMNSQueueMeta new];
        [AliMNSHttpBodyDecoder getQueueMetaFromXml:res.data withOutputQueueMeta: queueMeta];
    }
    else
    {
        [super parseErrorCode:res];
    }
}

@end

@implementation  AliMNSSendMessageResponse

@synthesize message;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"201"])
    {
        message = [AliMNSMessage new];
        [AliMNSHttpBodyDecoder getMessageFromXml:res.data withOutputMessage:message];
        message.requestId = [res.header objectForKey:@"x-mns-request-id"];
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSBatchSendMessageResponse

@synthesize messageList;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"201"])
    {
        NSMutableArray* mList = [NSMutableArray new];
        [AliMNSHttpBodyDecoder getMessageListFromXml:res.data withOutputMessageList:mList];
        messageList = [mList copy];
    }
    else
    {
        [super parseErrorCode:res];
    }

}

@end

@implementation AliMNSPeekMessageResponse
@synthesize message;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        message = [AliMNSMessage new];
        [AliMNSHttpBodyDecoder getMessageFromXml:res.data withOutputMessage:message];
        message.requestId = [res.header objectForKey:@"x-mns-request-id"];
    }else if ( [res.status isEqual: @"404"])
    {
        // return nil message if error is 404.
        message = nil;
    }
    else
    {
        [super parseErrorCode:res];
    }
}

@end

@implementation AliMNSBatchPeekMessageResponse
@synthesize messageList;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        NSMutableArray* mList = [NSMutableArray new];
        [AliMNSHttpBodyDecoder getMessageListFromXml:res.data withOutputMessageList:mList];
        messageList = [mList copy];
    }
    else if ( [res.status isEqual: @"404"])
    {
        // return nil messageList if error is 404.
        messageList = nil;
    }
    else
    {
        [super parseErrorCode:res];
    }

}
@end

@implementation AliMNSReceiveMessageResponse
@synthesize message;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        message = [AliMNSMessage new];
        [AliMNSHttpBodyDecoder getMessageFromXml:res.data withOutputMessage:message];
        message.requestId = [res.header objectForKey:@"x-mns-request-id"];
    }
    else if ( [res.status isEqual: @"404"])
    {
        // return nil message if error is 404.
        message = nil;
    }
    else
    {
        [super parseErrorCode:res];
    }

}
@end

@implementation AliMNSBatchReceiveMessageResponse
@synthesize messageList;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        NSMutableArray* mList = [NSMutableArray new];
        [AliMNSHttpBodyDecoder getMessageListFromXml:res.data withOutputMessageList:mList];
        messageList = [mList copy];
    }
    else if ( [res.status isEqual: @"404"])
    {
        // return nil messageList if error is 404.
        messageList = nil;
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSDeleteMessageResponse
-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"204"])
    {
        //NSLog(@"Delete message successfully!\n");
    }
    else
    {
        [super parseErrorCode:res];
    }

}
@end

@implementation AliMNSBatchDeleteMessageResponse
-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"204"])
    {
        //NSLog(@"Batch Delete message successfully!\n");
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSChangeMessageVisibilityResponse
@synthesize receiptHandle;
@synthesize nextVisibleTime;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        NSString* rHandle = [NSString new];
        NSString* nextTime = [NSString new];
        [AliMNSHttpBodyDecoder getNextVisibilityFromXml:res.data withOutputNextVisibilityTime:&nextTime withOutputReceiptHandle: &rHandle];
        receiptHandle = rHandle;
        nextVisibleTime = [nextTime integerValue];
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSCreateTopicResponse
@synthesize topicUrl;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"201"] || [res.status isEqual:@"204"])
    {
       topicUrl = [res.header objectForKey:@"Location"];
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSDeleteTopicResponse

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual:@"204"])
    {
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSListTopicResponse
@synthesize topicList;
@synthesize nextMarker;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        NSString* marker = @"";
        NSMutableArray * tList = [NSMutableArray new];
        [AliMNSHttpBodyDecoder getTopicListFromXml:res.data withOutputMarker:&marker withOutputTopicList:&tList];
        nextMarker = marker;
        topicList = [tList copy];

    }else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSSetTopicAttributesResponse
-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"204"])
    {
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSGetTopicAttributesResponse
@synthesize topicMeta;
-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        topicMeta = [AliMNSTopicMeta new];
        [AliMNSHttpBodyDecoder getTopicMetaFromXml:res.data withOutputTopicMeta:topicMeta];
    }
    else
    {
        [super parseErrorCode:res];
    }
}

@end

@implementation AliMNSPublishMessageResponse
@synthesize message;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"201"])
    {
        message = [[AliMNSTopicMessage alloc] init];
        [AliMNSHttpBodyDecoder getMessageFromXml:res.data withOutputMessage:message];
        message.requestId = [res.header objectForKey:@"x-mns-request-id"];
    }
    else
    {
        [super parseErrorCode:res];
    }
}

@end

@implementation AliMNSSubscribeResponse
@synthesize subscriptionUrl;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"201"] || [res.status isEqual:@"204"])
    {
        subscriptionUrl = [res.header objectForKey:@"Location"];
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSUnsubcribeResponse
-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual:@"204"])
    {
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end

@implementation AliMNSListSubscriptionResponse
@synthesize subscriptionUrlList;
@synthesize nextMarker;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"200"])
    {
        NSString* marker = @"";
        NSMutableArray * subList = [NSMutableArray new];
        [AliMNSHttpBodyDecoder getSubscriptionListFromXml:res.data withOutputMarker:&marker withOutputSubscriptionList:&subList] ;
        nextMarker = marker;
        subscriptionUrlList = [subList copy];
    }else
    {
        [super parseErrorCode:res];
    }
}

@end

@implementation AliMNSSetSubscriptionAttributesResponse

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];
    if ([res.status isEqual: @"204"])
    {
    }
    else
    {
        [super parseErrorCode:res];
    }
}

@end

@implementation AliMNSGetSubscriptionAttributesResponse
@synthesize subscriptionMeta;

-(void) parseFromInternalHttpResponse:(AliMNSHttpResponseInternal*) res
{
    [super parseFromInternalHttpResponse:res];

    if ([res.status isEqual: @"200"])
    {
        subscriptionMeta = [AliMNSSubscription new];
        [AliMNSHttpBodyDecoder getSubscriptionFromXml:res.data withOutputSubscriptionMeta:subscriptionMeta];
    }
    else
    {
        [super parseErrorCode:res];
    }
}
@end
